package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC888 {
    public long startExecuteTime = System.currentTimeMillis();

    class Solution {
        public int[] fairCandySwap(int[] A, int[] B) {
            Set<Integer> setA = new HashSet<Integer>(Arrays.asList(Arrays.stream(A).boxed().toArray(Integer[]::new)));
            Set<Integer> setB = new HashSet<Integer>(Arrays.asList(Arrays.stream(B).boxed().toArray(Integer[]::new)));
            int sumA = 0, sumB = 0;
            for (int i : A) {
                sumA += i;
            }
            for (int i : B) {
                sumB += i;
            }

            int diffAToB = (sumA - sumB) / 2;
            for (int i : A) {
                if (setB.contains(i - diffAToB)) return new int[]{i, i - diffAToB};
            }

            return new int[]{};
        }
    }

    public void run() {
        Solution solution = new Solution();
        //输入：A = [1,2,5], B = [2,4]
        //输出：[5,4]
        int[] a = InputUtil.toIntegerArray("[2]");
        int[] b = InputUtil.toIntegerArray("[1,3]");

        System.out.println(Arrays.toString(solution.fairCandySwap(a, b)));
    }

    public static void main(String[] args) throws Exception {
        LC888 an = new LC888();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
